import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		contentList: [],
		totalPages: 1,
		pageIndex: 1,
		checkBox: "checkBox",
		noFavorite: "noFavoriteHide",
		buttonName: "编辑",
		statusAll: "no",
		eStatus: "no",
		operation: "operationHide",
		noData: "noData",   //noData无数据 
		showNoData: false,
		count: 0, //删除几个
	},
	edit(event) {   //编辑
		var checkBox;
		var editClass;
		var buttonName;
		var eStatus = event.target.dataset.status;
		var operation;
		if (eStatus == "yes") {
			checkBox = "checkBox";
			editClass = "";
			buttonName = "编辑";
			eStatus = "no";
			operation = "operationHide";
			//将选中的去掉
			var contentList = this.data.contentList;
			for (var i in contentList) {
				contentList[i].status = 0;
				contentList[i].checkImg = "../../img/checkICON.png";
			}
			this.setData({
				'contentList': contentList,
				count: 0,
			});
		} else {
			checkBox = "checkBoxShow";
			editClass = "editClass";
			buttonName = "取消";
			eStatus = "yes";
			operation = "operationShow";
		}
		this.setData({
			checkBox: checkBox,
			editClass: editClass,
			buttonName: buttonName,
			eStatus: eStatus,
			operation: operation
		})
	},
	checkThis(event) {
		var index;
		var content
		var count = this.data.count;
		var contentList = this.data.contentList;
		if (event != undefined) {
			index = event.target.dataset.index;
			content = contentList[index];
			var status = content.status;
			if (status == "0") {
				content.checkImg = "../../img/check.png";
				content.status = "1";
				count++;
			} else {
				content.checkImg = "../../img/checkICON.png";
				content.status = "0";
				count--;
			}
			console.log("status:" + status);
			console.log(count);
			contentList[index] = content;
		}

		this.setData({
			'contentList': contentList,
			count: count,
		});
	},

	checkAll(event) {
		var statusAll = event.target.dataset.statusall;
		var contentList = this.data.contentList;
		for (var index in contentList) {
			var content = contentList[index];
			if (statusAll == "yes") {
				content.checkImg = "../../img/checkICON.png";
				content.status = "0";   
			} else {
				content.checkImg = "../../img/check.png";
				content.status = "1";
			}
			contentList[index] = content;
		}
		var count;
		if (statusAll == "yes") {
			statusAll = "no";
			count = 0;
		} else {
			statusAll = "yes";
			if (contentList.length > 0) {
				count = contentList.length;
			}
		}
		this.setData({
			'contentList': contentList,
			'statusAll': statusAll,
			count: count,
		});
	},
	newsDetail(event) {
		wx.navigateTo({
			url: '../newsDetail/newsDetail?assetId=123&folderId=456'
		})
	},
	articleDetail(e) {
		var folderCode = 0;
		var type = e.currentTarget.dataset.type;
		var code = e.currentTarget.dataset.code;
		var contentType = e.currentTarget.dataset.cnttype;
		var name = e.currentTarget.dataset.name;
		var detailUrl = "";
		if (1 == contentType) {
			detailUrl = this.getUrl(type, code, folderCode, name);
		} else if (3 == contentType) {
			//支部通知公告
			// detailUrl = "../noticeDetail/noticeDetail?noticeId=" + code;
		} else if (2 == contentType) {
			//支部新闻
			detailUrl = "../partyNewsDetail/partyNewsDetail?newsId=" + code;
		}
		if (detailUrl.length > 0) {
			wx.navigateTo({
				url: detailUrl
			});
		} else {
			this.showPageMsg("tips", "该内容类型详情页暂未开发");
		}
	},
	onLoad() {
		this.loadList();
	},
	onReachBottom() {
		// 页面被拉到底部
		if (this.data.pageIndex > this.data.totalPages) {
			this.showPageMsg("tips", "没有更多内容了");
		} else {
			this.loadList();
		}
	},
	onPullDownRefresh: function() {
		this.setData({
			pageIndex: 1,
			contentList: []
		});
		this.loadList();
		wx.stopPullDownRefresh();
	},
	loadList: function() {
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentFavorList";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = "10";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "我的收藏加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var contents = respData.data.favors.content;
					var pageIndex = respData.data.favors.pageIndex + 1;
					var totalPages = respData.data.favors.totalPages;
					var contentList = [];
					for (var index in contents) {
						var content = contents[index];
						var json = {};
						var thumbnail = content.thumbnail;
						if (null == thumbnail || "" == thumbnail) {
							var imageFiles = content.imageItems;
							for (var j in imageFiles) {
								thumbnail = imageFiles[j].url;
								break;
							}
						}
						json["name"] = content.name;
						var type = content.type;
						if ("Pic" == type) {
							//图文类的资产默认是已完成
							json["compelte"] = '已完成';
						} else {
							//当前播放的时长>=总时长
							if (content.duration >= content.timeLong) {
								json["compelte"] = '已完成';
							} else {
								json["compelte"] = '未完成';
							}
						}
						json["url"] = thumbnail;
						json["code"] = content.code;
						json["type"] = type;
						json["contentType"] = content.contentType;
						json["checkImg"] = '../../img/checkICON.png';
						json["status"] = '0';
						json["sourceFrom"] = null == content.sourceFrom ? "华数" : content.sourceFrom;
						contentList.push(json);
					}
					var oldContentList = this.data.contentList;
					contentList = oldContentList.concat(contentList);
					this.setData({
						"contentList": contentList,
						"pageIndex": pageIndex,
						"totalPages": totalPages,
						"showNoData": contentList.length > 0 ? false : true,
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},
	delete: function() {
		var contentList = this.data.contentList;
		if (null != contentList && contentList.length > 0) {
			var contentArray = [];
			var newContentList = [];
			for (var index in contentList) {
				var content = contentList[index];
				if (1 == content.status) {
					var json = {};
					json["code"] = content.code;
					json["contentType"] = content.contentType;
					json["contentName"] = content.name;
					if ('' != content.contentType) {
						json["siteCode"] = getApp().globalData.siteCode;
					} else {
						json["siteCode"] = 0;
					}
					json["status"] = "0";
					json["columnCode"] = "";
					contentArray.push(json);
				} else {
					newContentList.push(content);
				}
			}
			if (contentArray.length > 0) {
				for (var index in contentArray) {
					var record = contentArray[index];
					var params = JSON.stringify(record);
					this.favorContent(params);
				}
				this.setData({
					checkBox: "checkBox",
					editClass: "",
					buttonName: "编辑",
					eStatus: "no",
					operation: "operationHide",
					contentList: newContentList,
					count: 0,
				});
			} else {
				this.showPageMsg("tips", "请选择至少一条记录。");
			}
		}
	},
	favorContent: function(params) {
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentFavor";
		this.httpGet(newUrl, params, "删除收藏操作失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					this.showPageMsg("tips", "收藏删除成功")
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	}
});
